// pages/index/Sunburn/Sunburn.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: "https://itwjy.gitee.io/tu/",
    tempFilePaths:[],
     images: [], 
     uploadedImages: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var objectId = options.objectId;
    console.log(objectId);
  },
  chooseImage: function () {        // 选择图片
    var that = this;
    wx.chooseImage({
      sizeType: ['compressed'],
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths; 
        console.log(tempFilePaths);
        that.setData({
          images: that.data.images.concat(tempFilePaths)
                });
      }
        })
  },  

  previewImage: function () {        // 预览图集
    var that = this;
    wx.previewImage({
      urls: that.data.images
    });
  },
  submit: function () {        // 提交图片，事先遍历图集数组
    var that = this;
    that.data.images.forEach(function (tempFilePath) {
      new AV.File('file-name', {
        blob: {
          uri: tempFilePath,
        },
      }).save().then(                // file => console.log(file.url())
        function (file) {                    // 先读取
          var uploadedImages = that.data.uploadedImages;
          uploadedImages.push(file.url());                    // 再写入
          that.setData({
            uploadedImages: uploadedImages
          }); console.log(uploadedImages);
        }
      ).catch(console.error);
    });
    wx.showToast({
      title: '评价成功',
       success: function () {
        wx.navigateBack();
      }
    });
  },
  delete: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var images = that.data.images;
    images.splice(index, 1);
    that.setData({
      images: images
    });
  },



  
  // chooseimage:function(){
  //   var _this=this;
  //   wx.chooseImage({
  //     count: 5,
  //     sizeType:['original','compressed'],
  //     sourceType:['album','camera'],
  //     success: function(res) {
  //       var tempFilePaths =res.tempFilePaths;
  //       console.log(tempFilePaths);
  //       _this.setData({
  //         images:_this.data.images.concat(tempFilePaths)
  //       })
  //     },
  //   })
  // },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})